<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .errorInfo{
            color: red;
            font-size: 12px;
            height: 20px;
        }
    </style>

    <script src="/html/js/axios.min.js"></script>
    <script src="/html/js/util.js"></script>
    <script>
        async function add(){
            var param = {
               name:$("name").value,
                phone:$("phone").value,
                money:$("money").value,
                email:$("email").value
            };
            var info = await ajaxPost("/user/add",param);
            if(info == "ok"){
                alert("添加成功");
            }
            else {
                console.log(info);
                //清空提示div
                var divArray = document.getElementsByClassName("errorInfo");
                for(var  i=0;i<divArray.length;i++){
                    divArray[i].innerHTML = "";
                }
                //将错误信息显示在div提示框中
                for(var i=0;i<info.length;i++){
                    var obj = info[i];
                    $(obj.field+"Div").innerHTML = obj.defaultMessage;
                }
            }
        }


        async function update(){
            var param = {
                money:$("newmoney").value,
                pwd:$("newpwd").value
            };
            var info = await ajaxPost("/user/update",param);
            if(info == "ok"){
                alert("修改成功");
            }
            else {
                console.log(info);
                //清空提示div
                var divArray = document.getElementsByClassName("errorInfo");
                for(var  i=0;i<divArray.length;i++){
                    divArray[i].innerHTML = "";
                }
                //将错误信息显示在div提示框中
                for(var i=0;i<info.length;i++){
                    var obj = info[i];
                    $("new"+obj.field+"Div").innerHTML = obj.defaultMessage;
                }
            }
        }
    </script>
</head>
<body>
    姓名：<input type="text" id="name">
    <div id="nameDiv" class="errorInfo"></div>
    工资：<input type="text" id="money"><br>
    <div id="moneyDiv" class="errorInfo"></div>
    电话：<input type="text" id="phone"><br>
    <div id="phoneDiv" class="errorInfo"></div>
    邮箱：<input type="text" id="email"><br>
    <div id="emailDiv" class="errorInfo"></div>
    <input type="button" value="添加" onclick="add()">

    <hr/>
    请输入新密码：<input type="text" id="newpwd"><br>
    <div id="newpwdDiv" class="errorInfo"></div>
    请输入新工资：<input type="text" id="newmoney"><br>
    <div id="newmoneyDiv" class="errorInfo"></div>
    <input type="button" value="修改" onclick="update()">
</body>
</html>